//
//  TableHeaderView.m
//  Lucky28
//
//  Created by zr on 2018/7/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "TableHeaderView.h"
#import "ZYBannerView.h"

@interface TableHeaderView ()<ZYBannerViewDataSource, ZYBannerViewDelegate>

@property (nonatomic, strong) ZYBannerView *banner;
@property (nonatomic, weak)UIImageView * buttonImageView;
@property (nonatomic, weak)UIImageView * searchImageView;
@property (nonatomic, weak)UILabel * searchLabel;
@property (nonatomic, weak)UIImageView * hotImageView;

@property (nonatomic, strong)UIButton * oneButton;
@property (nonatomic, strong)UIButton * twoButton;
@property (nonatomic, strong)UIButton * threeButton;
@property (nonatomic, strong)UIButton * fourButton;

@property (nonatomic, weak)UIView * bottomView;

@end

@implementation TableHeaderView



- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    // 配置banner
    [self setupBanner];

    UIImageView * buttonImageView = [UIImageView new];
    
    buttonImageView.image = ImageNamed(@"gfh");
    buttonImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    
    [buttonImageView addGestureRecognizer:tapGR];
    
    self.buttonImageView = buttonImageView;
    
    [self addSubview:self.buttonImageView];
    
    
    UIImageView * searchImageView = [UIImageView new];
    
    searchImageView.image = ImageNamed(@"hg");
    searchImageView.userInteractionEnabled = YES;
    
    self.searchImageView = searchImageView;
    
    [self.buttonImageView addSubview:self.searchImageView];
    
    
    UILabel * searchLabel = [UILabel new];
    
    searchLabel.textColor = ColorWithRGB(0xA1A1A1);
    searchLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    searchLabel.text = @"搜索用户或文章";
    
    self.searchLabel = searchLabel;
    
    [self.buttonImageView addSubview:self.searchLabel];
    
    
    UIImageView * hotImageView = [UIImageView new];
    
    hotImageView.image = ImageNamed(@"hot");
    
    self.hotImageView = hotImageView;
    
    [self addSubview:self.hotImageView];
    
    
    self.oneButton = [self setupButtonWith:@"qweq"];
    
    [self addSubview:self.oneButton];
    
    self.twoButton = [self setupButtonWith:@"wqeqe"];
    
    [self addSubview:self.twoButton];
    
    self.threeButton = [self setupButtonWith:@"qw"];
    
    [self addSubview:self.threeButton];
    
    self.fourButton = [self setupButtonWith:@"w"];
    
    [self addSubview:self.fourButton];
    
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = ColorWithRGB(0xf2f2f2);
    
    self.bottomView = bottomView;
    
    [self addSubview:self.bottomView];
}


- (void)layoutSubviews{
    
    [self.buttonImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(190 * SCREENH6);
        make.leading.mas_equalTo(self).offset(35 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-35 * SCREENH6);
        make.height.mas_equalTo(57 * SCREENH6);
    }];
    
    [self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.buttonImageView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.buttonImageView).offset(15 * SCREENH6);
        make.width.height.mas_equalTo(22.5 * SCREENH6);
    }];
    
    [self.searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.searchImageView.mas_trailing).offset(7 * SCREENH6);
        make.centerY.mas_equalTo(self.searchImageView);
    }];
    
    [self.hotImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.buttonImageView.mas_bottom).offset(5 * SCREENH6);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(246 * SCREENH6);
        make.height.mas_equalTo(14 * SCREENH6);
    }];
    
    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.hotImageView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(165 * SCREENH6);
        make.height.mas_equalTo(82 * SCREENH6);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.width.height.mas_equalTo(self.oneButton);
    }];
    
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneButton.mas_bottom).offset(10 * SCREENH6);
        make.leading.width.height.mas_equalTo(self.oneButton);
    }];
    
    [self.fourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.threeButton);
        make.trailing.width.height.mas_equalTo(self.twoButton);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(8 * SCREENH6);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)string{
    
    UIButton * button = [UIButton new];
    
    [button setImage:ImageNamed(string) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(ClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return button;
}


- (void)setupBanner
{
    // 初始化
    self.banner = [[ZYBannerView alloc] init];
    self.banner.dataSource = self;
    self.banner.delegate = self;
    self.banner.shouldLoop = YES;
    self.banner.autoScroll = YES;
    self.banner.pageControlFrame = CGRectMake(200, 195, 45, 32);
    
    [self addSubview:self.banner];
    
    // 设置frame
    self.banner.frame = CGRectMake(0,0,SCREEN_WIDTH,205 * SCREENH6);
}


#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return 3;//self.dataArray.count;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    
//    NSString * imageName = self.dataArray[index];
//    NSURL * url = [NSURL URLWithString:imageName];
//    //    NSLog(@"url==%@",url);
//    // 创建将要显示控件
//    UIImageView *imageView = [[UIImageView alloc] init];
//    //    imageView.image = [UIImage imageNamed:imageName];
//    [imageView sd_setImageWithURL:url];
//    //    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"轮播1"]];
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
//
//    return imageView;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    imageView.image = [UIImage imageNamed:@"3"];
    
    return imageView;
}


- (void)tapAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToSearchVC" object:nil];
}


- (void)ClickButton:(UIButton *)button{
    
    NSString * code;
    
    if (button == self.oneButton) {
        
        code = @"one";
    }else if (button == self.twoButton){
        
        code = @"two";
    }else if (button == self.threeButton){
        
        code = @"three";
    }else if (button == self.fourButton){
        
        code = @"four";
    }
    
    NSDictionary * dict = @{@"button":code};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToInfoVC" object:nil userInfo:dict];
}


#pragma mark - ZYBannerViewDelegate
// 在这里实现点击事件的处理
- (void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index
{
    //    NSLog(@"点击了第%ld个项目", (long)index);
}

- (void)banner:(ZYBannerView *)banner didScrollToItemAtIndex:(NSInteger)index
{
    //    NSLog(@"滚动到第%ld个项目", (long)index);
}



@end
